﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ServiceLocation;
using NHibernate.Validator.Engine;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Infrastructure.Tools.NHValidator;

namespace YoProgramo.Domain.BusinessLogic.Service
{
    public class MarkAsSpamService<Ttentity> : IMarkAsSpamService<Ttentity> {

        private IMarkAsSpamRepository<Ttentity> _repository;

        public MarkAsSpamService(IMarkAsSpamRepository<Ttentity> repository)
        {
            _repository = repository;
        }

        public MarkAsSpamService()
        {
            _repository = ServiceLocator.Current.GetInstance<IMarkAsSpamRepository<Ttentity>>();
        }

        #region IMarkAsSpamService<Ttentity> Members

        public IList<IMarkAsSpam<Ttentity>> GetAll() {
            return _repository.GetAll();
        }

        public NHibernate.Validator.Engine.InvalidValue[] Create(IMarkAsSpam<Ttentity> obj) {
            InvalidValue[] validatorErrors = new NHValidatorWrapper<IMarkAsSpam<Ttentity>>().Validate(obj);

            if (validatorErrors.Count() < 1)
                _repository.SaveOrUpdate(obj);

            return validatorErrors;
        }

        public bool Update(IMarkAsSpam<Ttentity> obj) {
            try
            {
                _repository.SaveOrUpdate(obj);
                return true;
            }
            catch
            {
                return false;
            }
            
        }

        public void Delete(IMarkAsSpam<Ttentity> obj) {
            _repository.Delete(obj);
        }

        public void SetSpam(IMarkAsSpam<Ttentity> obj, bool approved) {
            throw new NotImplementedException();
        }

        public bool IsSpammer(IUser user) {
            foreach (IMarkAsSpam<Ttentity> spam in GetAllHasSpammerAproved())
            {
                if (((IEntityCommon)spam.SpamFor).ByUser == user) return true;
            }
            return false;
        }

        public bool IsSpam(Ttentity obj) {
            foreach (IMarkAsSpam<Ttentity> spam in GetAll()) {
                if (((IEntityCommon)spam.SpamFor).Id == ((IEntityCommon)obj).Id) return true;
            }
            return false;
        }


        public IList<IMarkAsSpam<Ttentity>> GetAllHasSpammerAproved() {
            return (from s in GetAll()
                    where s.Approved
                    select s).ToList();
        }

        public IList<IMarkAsSpam<Ttentity>> GetAllBy(Ttentity obj) {
            return (from s in GetAll()
                    where ((IEntityCommon)s.SpamFor).Id == ((IEntityCommon)obj).Id
                    select s).ToList();
        }

        public IList<IMarkAsSpam<Ttentity>> GetAllAprovedBy(Ttentity obj) {
            return (from s in GetAll()
                    where ((IEntityCommon)s.SpamFor).Id == ((IEntityCommon)obj).Id && s.Approved
                    select s).ToList();
        }

        #endregion
    }
}